﻿using System;
using System.Collections;
using System.Xml;
using InfoJetSoft.Service.Entity;
using InfoJetSoft.Service.Util;

namespace InfoJetSoft.Service.Worker
{
	
	class XmlXmlToEditContainerWorker
	{
		
		public static void  parseContainer(XmlToEditContext context)
		{
			XmlToEdit xmlToEdit = context.XmlToEdit;
			string container = xmlToEdit.Container;
			if (container == null || container.Length <= 0)
			{
				throw new InfoJetException("no container", "XXEC01");
			}
			
			XmlElement root = context.Doc.DocumentElement;
			XmlNodeList containerXmlNodeList = DOMUtil.selectNodes(root, container, 0, context.NsResolver);
			if (containerXmlNodeList.Count <= 0)
			{
				throw new InfoJetException("no container", "XXEC02");
			}
			
			ArrayList contextIdList = context.ContextIdList;
			XmlNode containerNode = XmlToEditUtil.findNodeByContextId(containerXmlNodeList, contextIdList);
			
			//对Recursive Section进行Containert调整.
			string item = xmlToEdit.Item;
			if (item == null || item.Length <= 0)
			{
				throw new InfoJetException("no item", "EU03");
			}
			if (item.StartsWith(container) && container.EndsWith(item.Substring(container.Length)))
			{
				if (!context.XmlToEditAction.Equals(JetConstant.CMP_xCollection + "::" + JetConstant.ACT_Insert))
				{
					containerNode = containerNode.ParentNode;
				}
			}
			
			if (containerNode == null)
			{
				throw new InfoJetException("container not in context", "XXEC03");
			}
			else
			{
				context.ContainerNode = containerNode;
			}
		}
	}
}